<?php

namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Bican\Roles\Models\Permission as BicanPermission;
use DB;
use Auth;
use Flash;
use App\Models\ActionAttributeTrait;
class Income extends Model
{
    
    public function insert($data)
    {
        return $info  = DB::table("income")->insert($data);
        
    }
    
     /**
     * @param $region
     * @return array
     * 后台功能
     */
    public function getWorksList()
    {
        $draw = request('draw', 1);/*获取请求次数*/
        $start = request('start', config('admin.global.list.start')); /*获取开始*/
        $length = request('length', config('admin.global.list.length')); ///*获取条数*/
        $sqlCount = "SELECT COUNT(*) AS `sum` FROM income";
        $count = DB::select($sqlCount);
        $sql = "SELECT * FROM income";

       $sql .= " ORDER BY id DESC
                LIMIT ?
                OFFSET ?";
        $list = DB::select($sql, array($length, $start));
        //return $list;
        $list = json_encode($list, true);
        $list = json_decode($list, true);
                
        if ($list) {
            foreach ($list as &$v) {
               
                 $v['wincomebutton'] = self::getWorksNameButton($v);     
            }
        }
        return [
            'draw' => $draw,
            'recordsTotal' => $count[0]->sum,
            'recordsFiltered' => $count[0]->sum,
            'data' => $list,
        ];
    }

    
    
    public function showindex()
    {
        
        $draw = request('draw', 1);/*获取请求次数*/
        $start = request('start', config('admin.global.list.start')); /*获取开始*/
        $length = request('length', config('admin.global.list.length')); ///*获取条数*/
        $sqlCount = "SELECT COUNT(*) AS `sum` FROM income";
        $count = DB::select($sqlCount);
        $sql = "SELECT * FROM income";
        $sql .= " ORDER BY id DESC
                LIMIT ?
                OFFSET ?";
           
        $list = DB::select($sql, array($length, $start));
        //return $list;
        $list = json_encode($list, true);
        $list = json_decode($list, true);
                
        if ($list) {
            foreach ($list as &$v) {
               
                 $v['wincomebutton'] = self::getWorksNameButton($v);     
            }
        }
        return [
            'draw' => $draw,
            'recordsTotal' => $count[0]->sum,
            'recordsFiltered' => $count[0]->sum,
            'data' => $list,
        ];
        
        
    }
    
    
    
    
    public function showall()
    {
         $draw = request('draw', 1);/*获取请求次数*/
        $start = request('start', config('admin.global.list.start')); /*获取开始*/
        $length = request('length', config('admin.global.list.length')); ///*获取条数*/
        $sqlCount = "SELECT COUNT(*) AS `sum` FROM income";
        $count = DB::select($sqlCount);
        $sql = "SELECT * FROM income";
        $sql .= " ORDER BY id DESC
                LIMIT ?
                OFFSET ?";
           
        $list = DB::select($sql, array($length, $start));
        //return $list;
        $list = json_encode($list, true);
        $list = json_decode($list, true);
                
        if ($list) {
            foreach ($list as &$v) {
               
                 $v['wincomebutton'] = self::getWorksNameButton($v);     
            }
        }
        return [
            'draw' => $draw,
            'recordsTotal' => $count[0]->sum,
            'recordsFiltered' => $count[0]->sum,
            'data' => $list,
        ];
        
    }
   public function getWorksNameButton($v)
    {
        return '<button class="btn btn-xs btn-primary tooltips btn-edit"  data-original-title="编辑" data-wname="' .
        $v['project'] . '" data-wtype="' .
        $v['service'] . '" data-hstype="' .
        $v['amount'] . '" data-hmode="' .
        $v['ticheng'] . '" data-cost="' .
        $v['activity'] . '" data-area="' .
        $v['other'] . '" data-place="' .
        $v['time'] . '" data-created="' . $v['created_at'] . '" data-id="' . $v['id'] . '" data-csrf="' . csrf_token() . '" data-placement="top">' . $v['project'] . '</button>';
    }
    
    
    
    
   
    
}
